<%--
  Created by IntelliJ IDEA.
  User: 王韫琛
  Date: 2021/1/18
  Time: 15:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>住户信息维护</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/webjars/layui/2.5.6/css/layui.css">
</head>
<body>




<div style="text-align: center; display:none" id="form_body">
    <div class="layui-inline">
        <form  class="layui-form" lay-filter="dataFrm" method="post">
            <input type="hidden" name="id">
            <div class="layui-form-item">
                <label class="layui-form-label" for="house_username" style="width: 120px">户主姓名:</label>
                <div class="layui-input-inline">
                    <input type="text" name="house_username" id="house_username" class="layui-input" required
                           lay-verify="required"
                           lay-reqText="请输入户主姓名" placeholder="请输入户主姓名" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="gender" style="width: 120px">性别:</label>
                <div class="layui-input-inline">
                    <input type="radio" name="gender" id="gender" value="男" title="男" checked="checked">
                    <input type="radio" name="gender" value="女" title="女">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="certificate_number" style="width: 120px">身份证号:</label>
                <div class="layui-input-inline">
                    <input type="text" name="certificate_number" id="certificate_number" class="layui-input" required
                           lay-verify="required|identity"
                           lay-reqText="身份证号" placeholder="身份证号" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="nation" style="width: 120px">民族:</label>
                <div class="layui-input-inline">
                    <input type="text" name="nation" id="nation" class="layui-input" required
                           lay-verify="required"
                           lay-reqText="请输入民族"
                           placeholder="请输入民族" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="test1" style="width: 120px">生日:</label>
                <div class="layui-input-inline">
                    <input type="text" name="birthday" required lay-verify="required|data" class="layui-input"
                           placeholder="请输入生日" id="test1">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="education" style="width: 120px">学历:</label>
                <div class="layui-input-inline">
                    <input type="text" name="education" id="education" class="layui-input" required
                           lay-verify="required"
                           lay-reqText="请输入学历" placeholder="请输入学历" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="house_number" style="width: 120px">门牌号:</label>
                <div class="layui-input-inline">
                    <input type="text" name="house_number" id="house_number" class="layui-input" required
                           lay-verify="required"
                           lay-reqText="请输入门牌号" placeholder="请输入门牌号" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="permanent_address" style="width: 120px">户籍地址:</label>
                <div class="layui-input-inline">
                    <input type="text" name="permanent_address" id="permanent_address" class="layui-input" required
                           lay-verify="required"
                           lay-reqText="请输入籍贯" placeholder="请输入籍贯" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="hobby" style="width: 120px">爱好:</label>
                <div class="layui-input-inline">
                    <input type="text" name="hobby" id="hobby" class="layui-input" required lay-verify="required"
                           lay-reqText="请输入爱好" placeholder="请输入爱好" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="home_phone" style="width: 120px">住宅电话</label>
                <div class="layui-input-inline">
                    <input type="text" name="home_phone" id="home_phone" class="layui-input" required
                           lay-verify="required|phone"
                           lay-reqText="请输入住宅电话" placeholder="请输入住宅电话" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="cell_phone_number" style="width: 120px">手机号:</label>
                <div class="layui-input-inline">
                    <input type="text" name="cell_phone_number" id="cell_phone_number" class="layui-input" required
                           lay-verify="required|phone"
                           lay-reqText="请输入手机号" placeholder="请输入手机号" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="work_unit" style="width: 120px">工作单位:</label>
                <div class="layui-input-inline">
                    <input type="text" name="work_unit" id="work_unit" class="layui-input" required
                           lay-verify="required"
                           lay-reqText="请输入工作单位" placeholder="请输入工作单位" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="work_telephone" style="width: 120px">单位电话:</label>
                <div class="layui-input-inline">
                    <input type="text" name="work_telephone" id="work_telephone" class="layui-input" required
                           lay-verify="required"
                           lay-reqText="请输入单位电话" placeholder="请输入单位电话" autocomplete="off">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label" for="unit_address" style="width: 120px">单位地址:</label>
                <div class="layui-input-inline">
                    <input type="text" name="unit_address" id="unit_address" class="layui-input" required
                           lay-verify="required"
                           lay-reqText="请输入单位地址" placeholder="请输入单位地址" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="emergency_contact" style="width: 120px">紧急联系人:</label>
                <div class="layui-input-inline">
                    <input type="text" name="emergency_contact" id="emergency_contact" class="layui-input" required
                           lay-verify="required"
                           lay-reqText="请输入紧急联系人" placeholder="请输入紧急联系人" autocomplete="off">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="emergency_contact_number" style="width: 120px">紧急联系人电话:</label>
                <div class="layui-input-inline">
                    <input type="text" name="emergency_contact_number" id="emergency_contact_number" class="layui-input"
                           required
                           lay-verify="required|phone"
                           lay-reqText="请输入紧急联系人电话" placeholder="请输入紧急联系人电话" autocomplete="off">
                </div>
            </div>
            <div class="layui-input-inline" style="padding-left: 45px">
                <button class="layui-btn" lay-submit lay-filter="doSubmit" lay-event="add">修改</button>
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
                <button type="reset" class="layui-btn layui-btn-warm">重置</button>
            </div>
        </form>
    </div>
</div>
<br>
<%--搜索表单开始--%>
<form class="layui-form layui-form-pane" method="post">
    &nbsp;搜索身份证：
    <div class="layui-inline">
        <input class="layui-input" name="certificate_number">
    </div>
    <button class="layui-btn" lay-submit lay-filter="doSearch">搜索</button>
</form>
<%--搜索表单结束--%>

<%--表格渲染开始--%>
<table class="layui-hide" id="test" lay-filter="test"></table>
<%--表格渲染结束--%>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
    </div>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/javascript" src="${pageContext.request.contextPath}/webjars/layui/2.5.6/layui.js"></script>

<script>
    layui.use(['form', 'jquery', 'table', 'laydate', 'layer'], function () {
        var form = layui.form;
        var $ = layui.jquery;
        var table = layui.table;
        var layer = layui.layer;
        var laydate = layui.laydate;
        //日期控件
        laydate.render({
            elem: '#test1' //指定元素
            ,format: 'yyyy-MM-dd'
        });
        //表单渲染开始
        var tableIns = table.render({
            elem: '#test'
            , height: 524
            , url: '/adminFindPage'
            , page: true //开启分页
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: ['filter', 'exports', 'print']
            , title: '用户数据表'
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id', title: 'ID', width: 80, fixed: 'left', unresize: true, sort: true}
                , {field: 'house_username', title: '户主姓名', width: 120, edit: 'text'}
                , {field: 'gender', title: '性别', align: "center"}
                , {field: 'certificate_number', title: '身份证号', align: "center"}
                , {field: 'nation', title: '民族', align: "center"}
                , {field: 'birthday', title: '生日', align: "center"}
                , {field: 'education', title: '学历', align: "center"}
                , {field: 'house_number', title: '门牌号', align: "center",sort: true}
                , {field: 'permanent_address', title: '户籍地址', align: "center"}
                , {field: 'hobby', title: '爱好', align: "center"}
                , {field: 'home_phone', title: '住宅电话', align: "center"}
                , {field: 'cell_phone_number', title: '手机号', sort: true, align: "center"}
                , {field: 'work_unit', title: '工作单位', align: "center"}
                , {field: 'work_telephone', title: '单位电话', align: "center"}
                , {field: 'emergency_contact', title: '紧急联系人', align: "center"}
                , {field: 'emergency_contact_number', title: '紧急联系人电话', align: "center"}
                , {fixed: 'right', title: '操作', toolbar: '#barDemo', align: "center"}
            ]]
            , request: {
                pageName: 'pageNum' // 页码的参数名称，默认：page
                , limitName: 'pageSize' //每页数据量的参数名，默认：limit
            }
            , id: 'testReload'
        });
        //表单渲染结束

        //头工具栏事件开始
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：' + data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选' : '未全选');
                    break;
            }
            ;
        });
        //头工具栏事件结束

        //监听行工具事件开始
        table.on('tool(test)', function (obj) {
            switch (obj.event) {
                //编辑
                case 'edit':
                    openUpdateWindow(obj.data);
                    break;
                //删除
                case 'del':
                    deleteById(obj.data);
                    break;
            }
        });
        //监听行工具事件结束

        var mainIndex;//窗口索引
        var url;//提交地址
        //更新用户信息开始
        function openUpdateWindow(data) {
            console.log(data)
            mainIndex = layer.open({
                type: 1,//弹出层类型
                title: "修改住户信息",
                area: ['600px', '600px'],
                content: $("#form_body"),//引用的窗口代码

                success: function () {
                    //表单数据回显
                    form.val("dataFrm", data);
                    //修改请求
                    url = "/updateHouseholdInformationSheet";
                }
            });
        }
        //更新用户信息结束
        //删除用户开始
        function deleteById(data) {
            //提示用户确认是否删除
            layer.confirm("确定要删除么", {icon: 3, title: "提示"}, function (index) {
                //发送ajax请求
                $.post("/deleteHouseholdInformationSheetById", {"id": data.id}, function (result) {
                    if (result.success) {
                        layer.alert(result.message, {icon: 1});
                        //刷新数据表格
                        tableIns.reload();
                    } else {
                        layer.alert(result.message, {icon: 2});
                    }
                }, "json");
                //关闭提示框
                layer.close(index);
            });
        }
        //删除用户信息结束

        //标识是否存在
        var flag = false;
        var flag2 = true;
        //检测身份证开始
        $("#certificate_number").blur(function () {
            //获取输入的身份证号
            let certificate_number = $(this).val().trim();
            console.log(certificate_number);
            //判断是否为空
            if (certificate_number.length > 0) {
                $.get("/findByCertificateNumber", {"certificate_number": certificate_number}, function (result) {
                    if (result.success) {
                        layer.tips(result.message, "#certificate_number");
                        flag = true;
                    } else {
                        flag = false;
                        layer.tips(result.message, "#certificate_number");
                    }
                }, "json");
            }
        });
        //检测身份证结束


        //监听表单提交事件开始
        form.on("submit(doSubmit)", function (data) {
            if(flag&&flag2){
                layer.msg("身份证或手机号");
            }else {
                $.post(url, data.field, function (result) {
                    if (result.success) {
                        layer.alert(result.message, {icon: 1});
                        //关闭窗口
                        layer.close(mainIndex);
                        //刷新数据表格
                        tableIns.reload();
                    } else {
                        layer.alert(result.message, {icon: 2});
                    }
                }, "json");
            }
            return false;
        });
        //监听表单提交事件结束

        //检测手机号开始
        $("#cell_phone_number").blur(function () {
            //获取输入的用户名
            let cell_phone_number = $(this).val().trim();
            //判断是否为空
            if (cell_phone_number.length > 0) {
                $.get("/findByCellPhoneNumber", {"cell_phone_number": cell_phone_number}, function (result) {
                    if (result.success) {
                        //layer.msg(result.message);
                        layer.tips(result.message, "#cell_phone_number");
                        flag = true;
                    } else {
                        flag = false;
                        layer.tips(result.message, "#cell_phone_number");
                    }
                }, "json");
            }
        });
        //检测手机号结束

        //监听搜索按钮得提交开始
        form.on("submit(doSearch)", function (data) {
            tableIns.reload({
                url: '/selectByCertificateNumber',
                where: data.field,//查询条件
                page: {
                    curr: 1
                }
            })
            //禁止页面刷新
            return false;
        })
        //监听搜索按钮结束
    });
</script>

</body>
</html>
